<%
layout("/layouts/platform.html"){
%>
<section class="content-wrap bg-white">
    <header class="header navbar bg-white shadow">
        <div class="btn-group tool-button">
            <a id="add" class="btn btn-primary navbar-btn" href="${base}/chain/transaction/add" data-pjax><i class="ti-plus"></i> 新建版权</a>
            <!--<button class="btn btn-danger navbar-btn" onclick="delCheck()"><i class="ti-close"></i> 删除选中</button>-->
        </div>
    </header>
    <div class=panel-body style="padding-top: 50px;">
        <div class="table-responsive no-border">
            <table class="table table-bordered table-striped mg-t datatable">
                <thead>
                    <tr>
                            <th>版权号</th>
                            <th>买家姓名</th>
                            <th>卖家姓名</th>
                            <th>区块链交易号</th>
                            <th>交易额</th>
                            <th>交易时间</th>
                            <th>备注</th>
                            <th>操作</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</section>
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">删除</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        删除后无法恢复。<br/>
                        <br/>确定删除吗？
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取  消</button>
                <button id="okDel" type="button" class="btn btn-primary" data-loading-text="正在删除...">确  定</button>
            </div>
        </div>
    </div>
</div>
<div id="dialogDetail" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>
<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": false,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/cn.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/chain/transaction/data",
                "type": "post",
                "data": function (d) {
                    //d.nutz = $('#nutz').val();
                }
            },
            "order": [[1, "desc"]],
            "columns": [
                    {"data": "t_copyright.code", "bSortable": false,"defaultContent":"","width":"15%"},
                    {"data": "bayer", "bSortable": true,"defaultContent":"","width":"10%"},
                    {"data": "seller", "bSortable": true,"defaultContent":"","width":"10%"},
                    {"data": "blockChainTransactionCode", "bSortable": true,"defaultContent":"","width":"15%"},
                    {"data": "amount", "bSortable": true,"defaultContent":"","width":"10%"},
                    {"data": "transactionTime", "bSortable": true,"defaultContent":"","width":"10%"},
                    {"data": "comment", "bSortable": true,"defaultContent":"","width":"15%"},
            ],

            "columnDefs": [
                {"render": function(data, type, row) {
                        if(data) {
                            var contents =row.t_copyright.code;
                            if(contents.length>20){
                                contents=contents.substring(0,20)+"...";
                            }
                            return "<div style=\"word-wrap:break-word;\" title=\""+row.t_copyright.code+"\">"+contents+"</div>";
                        }else
                            return "";
                    },
                    "targets": 0
                },
                {"render": function(data, type, row) {
                        if(data) {
                            var contents =row.blockChainTransactionCode;
                            if(contents.length>30){
                                contents=contents.substring(0,30)+"...";
                            }
                            return "<div style=\"word-wrap:break-word;\" title=\""+row.blockChainTransactionCode+"\">"+contents+"</div>";
                        }else
                            return "";
                    },
                    "targets": 3
                },
                {"render": function(data, type, row) {
                        if(data) {
                            var contents =row.comment;
                            if(contents.length>20){
                                contents=contents.substring(0,20)+"...";
                            }
                            return "<div style=\"word-wrap:break-word;\" title=\""+row.comment+"\">"+contents+"</div>";
                        }else
                            return "";
                    },
                    "targets": 6
                },
                {
                  "render": function (data, type, row) {
                           return  moment(data).format("YYYY-MM-DD");
                    },
                  "targets": 5
                },
                {
                      "render": function (data, type, row) {
                       if(row.isChain){
                           return '<a href="${base}/chain/transaction/detail/' + row.id + '" data-toggle="modal" data-target="#dialogDetail">查看</a>';
                        }else {
                           return '<a href="${base}/chain/transaction/detail/' + row.id + '" data-toggle="modal" data-target="#dialogDetail">查看</a>' +
                           '&nbsp;<a href="javascript:addChain(\''+row.id+'\');">上链</a>';
                       }

                    },
                    "targets": 7,
                    "width":"10%"
                }
            ]
        });
        datatable.on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }
    function del(id) {
        var dialog = $("#dialogDelete");
        dialog.modal("show");
        dialog.find("#okDel").unbind("click");
        dialog.find("#okDel").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/chain/transaction/delete/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null,false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }
    function delCheck() {
        var chks = datatable.rows('.selected').data();
        if (chks.length > 0) {
            var ids = [];
            $.each(datatable.rows('.selected').data(), function (i, n) {
                ids.push(n.id);
            });
            var dialog = $("#dialogDelete");
            dialog.modal("show");
            dialog.find("#okDel").unbind("click");
            dialog.find("#okDel").bind("click", function (event) {
                var btn = $(this);
                btn.button("loading");
                $.post("${base}/chain/transaction/delete", {ids: ids.toString()}, function (data) {
                    if (data.code == 0) {
                        datatable.ajax.reload(null,false);
                    } else {
                        Toast.error(data.msg);
                    }
                    btn.button("reset");
                    dialog.modal("hide");
                }, "json");
            });
        } else {
            Toast.warning("请先选择要删除的项！");
        }
    }
    $(function () {
        initDatatable();
        $("#dialogDetail").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
        });
    });

     function addChain(id) {
        $.ajax({
            type: "POST",
            url: "${base}/chain/transaction/addChain",
            dataType: "json",
            cache: false,  //禁用缓存
            data:{
                transactionId:id
            },
            success: function (data) {
                datatable.ajax.reload(null,false);
            }
        });
     }
</script>

<%}%>